import time
from datetime import datetime

import wtforms
from flask import flash
from wtforms.validators import length, Email, EqualTo, InputRequired,DataRequired
from modle import EmailCaptchaModel, UserModel
import re


# 登录表单
class LoginForm(wtforms.Form):
    email = wtforms.StringField('邮箱',validators=[length(min=3, max=50),Email("邮箱格式错误"),DataRequired('邮箱不能为空')])
    password = wtforms.PasswordField('密码',validators=[length(min=6,message='密码不得低于6位'),DataRequired('密码不能为空')])


# # 注册表单
class RegisterForm(wtforms.Form):
    reg_email = wtforms.StringField('邮箱',validators=[Email()])
    captcha = wtforms.StringField('验证码',validators=[length(min=4, max=4)])
    reg_password = wtforms.PasswordField('密码',validators=[length(min=6, max=20)])
    password_confirm = wtforms.PasswordField('确认密码',validators=[EqualTo('reg_password')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.reg_email.data
        captcha_model = EmailCaptchaModel.query.filter_by(email=email).first()
        if not captcha_model or captcha_model.captcha.lower() != captcha.lower():
            if len(captcha):
                flash('验证码错误',category='danger')
            raise wtforms.ValidationError('邮箱验证码错误')
        elif captcha_model.captcha.lower() == captcha.lower():
            now_time = time.mktime(datetime.now().timetuple())
            create_time = str(captcha_model.create_time)
            timeArray = time.strptime(create_time, "%Y-%m-%d %H:%M:%S")
            timestamp = time.mktime(timeArray)
            if now_time-timestamp>60:
                flash('验证码已经超时',category='danger')
                raise wtforms.ValidationError('邮箱验证码错误')
    #
    def validate_reg_email(self, field):
        email = field.data
        user_model = UserModel.query.filter_by(email=email).first()
        if user_model:
            flash('邮箱已存在',category='danger')
            raise wtforms.ValidationError('邮箱已经存在')

    def validate_password_confirm(self,field):
        password_confirm = field.data
        reg_password = self.reg_password.data
        if password_confirm != reg_password:
            flash('密码不一致',category='danger')
            raise wtforms.ValidationError('密码不一致')
#
#
# # 问答表单
# class QuestionForm(wtforms.Form):
#     title = wtforms.StringField(validators=[length(min=3, max=200)])
#     content = wtforms.StringField(validators=[length(min=5)])
#     category = wtforms.StringField(validators=[length(min=1)])
#
#
class AnswerForm(wtforms.Form):
    body = wtforms.StringField(validators=[length(min=1)])

    def validate_body(self,field):
        body = field.data
        if (re.findall('(我操|卧槽|我草泥马|他妈的|你麻痹|你他妈)',body)):
            flash('评论不要骂人哦','warning')
            raise wtforms.ValidationError('带脏话')
    # question_id = wtforms.IntegerField(validators=[InputRequired()])
#
#

class UpdateAdmForm(wtforms.Form):
    username = wtforms.StringField('用户名',validators=[length(min=1,max=20)])

class UpdateForm(wtforms.Form):
    reg_email = wtforms.StringField('邮箱', validators=[Email()])
    captcha = wtforms.StringField('验证码', validators=[length(min=4, max=4)])
    reg_password = wtforms.PasswordField('密码', validators=[length(min=6, max=20)])
    password_confirm = wtforms.PasswordField('确认密码', validators=[EqualTo('reg_password')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.reg_email.data
        captcha_model = EmailCaptchaModel.query.filter_by(email=email).first()
        if not captcha_model or captcha_model.captcha.lower() != captcha.lower():
            if captcha:
                flash('验证码错误', category='danger')
            raise wtforms.ValidationError('邮箱验证码错误')
        elif captcha_model.captcha.lower() == captcha.lower():
            now_time = time.mktime(datetime.now().timetuple())
            create_time = str(captcha_model.create_time)
            timeArray = time.strptime(create_time, "%Y-%m-%d %H:%M:%S")
            timestamp = time.mktime(timeArray)
            if now_time - timestamp > 60:
                flash('验证码已经超时', category='danger')
                raise wtforms.ValidationError('邮箱验证码错误')

    #
    def validate_reg_email(self, field):
        email = field.data
        user_model = UserModel.query.filter_by(email=email).first()
        if not user_model:
            flash('邮箱还未注册', category='danger')
            raise wtforms.ValidationError('该邮箱未注册')

    def validate_password_confirm(self, field):
        password_confirm = field.data
        reg_password = self.reg_password.data
        if password_confirm != reg_password:
            flash('密码不一致', category='danger')
            raise wtforms.ValidationError('密码不一致')
